package com.lw.common.customview

import android.app.Dialog
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.lw.common.Constants
import com.lw.common.R
import com.lw.common.databinding.PopwinBottomBinding
import com.lw.common.utils.MMKVUtils.put
import com.lw.common.webview.WebViewActivity

/**
 * @description: 第一次使用 温馨提示底部弹窗
 * @author: yuan
 * @date: 2024/12/16
 */
class MyBottomDialog : BottomSheetDialogFragment() {
    private var mListener: OnClickListener? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val binding = PopwinBottomBinding.inflate(inflater, container, false)

        val builder =
            SpannableStringBuilder("欢迎使用利万农村饮水节能增效系统，在使用利万农村饮水节能增效系统前，请认真阅读《用户协议》和《隐私权政策》。您需要同意并接受全部条款后再开始我们的服务。")
        val span: ClickableSpan = object : ClickableSpan() {
            override fun onClick(view: View) {
                WebViewActivity.start(requireActivity(), "用户协议")
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ContextCompat.getColor(requireActivity(), R.color.colorPrimary)
                ds.isUnderlineText = false //设置下划线
                ds.isFakeBoldText = true //设置粗体
            }
        }
        builder.setSpan(span, 39, 45, Spannable.SPAN_INCLUSIVE_EXCLUSIVE) //位置含头不含尾
        val span2: ClickableSpan = object : ClickableSpan() {
            override fun onClick(view: View) {
                WebViewActivity.start(requireActivity(), "隐私权政策")
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ContextCompat.getColor(requireActivity(), R.color.colorPrimary)
                ds.isUnderlineText = false //设置下划线
                ds.isFakeBoldText = true //设置粗体
            }
        }
        builder.setSpan(span2, 46, 53, Spannable.SPAN_INCLUSIVE_EXCLUSIVE) //位置含头不含尾
        binding.tipsText.text = builder
        // 去除点击时的背景色
        binding.tipsText.highlightColor = ContextCompat.getColor(binding.tipsText.context, android.R.color.transparent)
        // 点击事件生效
        binding.tipsText.movementMethod = LinkMovementMethod.getInstance()

        //设置列表的适配器
        binding.tipsDisagree.setOnClickListener { requireActivity().onBackPressed() }
        binding.tipsAgree.setOnClickListener {
            put(Constants.IS_FIRST, true)
            mListener!!.onClick()
            dismiss()
        }

        return binding.root
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return BottomSheetDialog(requireContext(), R.style.MyBottomDialogStyle)
    }

    interface OnClickListener {
        //在初始化时获得事件
        fun onClick()
    }

    fun setOnClickListener(listener: OnClickListener?) {
        mListener = listener
    }
}
